package com.activity.shop;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import event.MessageEvent;

import android.content.Intent;
import android.os.Bundle;

import com.activity.adapter.CreateSheetGroupAdapter;
import com.activity.adapter.TimeChooseAdapter;
import com.activity.bean.AddressBean;
import com.activity.bean.ChooseTimeBean;
import com.activity.bean.ClassBean;
import com.activity.bean.ClassSpan;
import com.activity.bean.EmployerBean;
import com.activity.bean.TimeBean;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.api.param.CategoryPagePram;
import com.api.param.GroupParam;
import com.api.viewModel.StoreVM;
import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.activity.setting.BaseListViewActivity;
import com.example.apple.ddhr_new.utils.StringUtil;
import com.example.apple.ddhr_new.utils.ToastUtil;
import com.google.gson.Gson;

import org.apache.commons.lang3.StringUtils;
import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Arrays;

public class CreateAttanceGroupActivity extends BaseListViewActivity<StoreVM> {


   private CreateSheetGroupAdapter createSheetGroupAdapter;
   private ClassBean classBean;

   @Override
   protected int titleText() {
      return R.string.xinjian_kaoqin_zu;
   }

   @Override
   protected void created(@Nullable Bundle savedInstanceState) {
      super.created(savedInstanceState);

      String param =getIntent().getExtras().getString("param");
      if(param==null){

      }else {
         Gson gson = new Gson();
         classBean = gson.fromJson(param,ClassBean.class);
         setTitleText("修改考勤组");
      }
      createSheetGroupAdapter = new CreateSheetGroupAdapter(this);
      if(classBean!=null){
         createSheetGroupAdapter.groupName =classBean.getAttendancegroupname();
         createSheetGroupAdapter.span = Integer.valueOf(classBean.getAttendancegroupradius());
         createSheetGroupAdapter.time = classBean.getAttendanceOvertimerange();
         if(classBean.getAttendancegroup_attendancetimespanid().equals("0")){

         }else {
            ClassSpan  classSpan = new ClassSpan();
            classSpan.selected =true;
            classSpan.setAttendacetimespan_begintime(classBean.getAttendancetimespan().get(0).getAttendacetimespan_begintime());
            classSpan.setAttendacetimespan_endtime(classBean.getAttendancetimespan().get(0).getAttendacetimespan_endtime());
            classSpan.setAttendacetimespan_storeno(classBean.getAttendancetimespan().get(0).getAttendacetimespan_storeno());
            classSpan.setAttendacetimespan_timelength(classBean.getAttendancetimespan().get(0).getAttendacetimespan_timelength());
            classSpan.setAttendacetimespanname(classBean.getAttendancetimespan().get(0).getAttendacetimespanname());
            classSpan.setAttendacetimespanid(classBean.getAttendancetimespan().get(0).getAttendacetimespanid());
            createSheetGroupAdapter.classSpan =classSpan;

         }
         if(classBean.getAttendanceregionlist()!=null&&classBean.getAttendanceregionlist().size()>0){

            for (int i=0;i<classBean.getAttendanceregionlist().size();i++){
               ClassBean.AttendanceregionlistBean attendanceregionlistBean = classBean.getAttendanceregionlist().get(i);
               LatLonPoint latLonPoint = new LatLonPoint(Double.valueOf(attendanceregionlistBean.getAttendanceregionlat()) , Double.valueOf(attendanceregionlistBean.getAttendanceregionlng()) );
               PoiItem poiItem = new PoiItem(null,latLonPoint,attendanceregionlistBean.getAttendanceregionname(),attendanceregionlistBean.getAttendanceregionaddress());
               createSheetGroupAdapter.getData().add(poiItem);

            }
         }
//         TimeBean day5 = new TimeBean("每周五","请选择班次");
//         TimeBean day6 = new TimeBean("每周六","休息");
         ArrayList<TimeBean> arrayList = new ArrayList<>();

         if(classBean.getAttendancegroup_day1_attendancetimespanid().equals("0")){

         }else
         {

            TimeBean day1 = new TimeBean("每周一","请选择班次");
            day1.selected =true;
            ClassSpan  classSpan = new ClassSpan();
            classSpan.setAttendacetimespan_begintime(classBean.getAttendancetimespan_day1().get(0).getAttendacetimespan_begintime());
            classSpan.setAttendacetimespan_endtime(classBean.getAttendancetimespan_day1().get(0).getAttendacetimespan_endtime());
            classSpan.setAttendacetimespan_storeno(classBean.getAttendancetimespan_day1().get(0).getAttendacetimespan_storeno());
            classSpan.setAttendacetimespan_timelength(classBean.getAttendancetimespan_day1().get(0).getAttendacetimespan_timelength());
            classSpan.setAttendacetimespanname(classBean.getAttendancetimespan_day1().get(0).getAttendacetimespanname());
            classSpan.setAttendacetimespanid(classBean.getAttendancetimespan_day1().get(0).getAttendacetimespanid());
            day1.setClassSpan(classSpan);
            arrayList.add(day1);
         }
         if(classBean.getAttendancegroup_day2_attendancetimespanid().equals("0")){

         }else
         {

            TimeBean day1 = new TimeBean("每周二","请选择班次");
            day1.selected =true;
            ClassSpan  classSpan = new ClassSpan();
            classSpan.setAttendacetimespan_begintime(classBean.getAttendancetimespan_day2().get(0).getAttendacetimespan_begintime());
            classSpan.setAttendacetimespan_endtime(classBean.getAttendancetimespan_day2().get(0).getAttendacetimespan_endtime());
            classSpan.setAttendacetimespan_storeno(classBean.getAttendancetimespan_day2().get(0).getAttendacetimespan_storeno());
            classSpan.setAttendacetimespan_timelength(classBean.getAttendancetimespan_day2().get(0).getAttendacetimespan_timelength());
            classSpan.setAttendacetimespanname(classBean.getAttendancetimespan_day2().get(0).getAttendacetimespanname());
            classSpan.setAttendacetimespanid(classBean.getAttendancetimespan_day2().get(0).getAttendacetimespanid());
            day1.setClassSpan(classSpan);
            arrayList.add(day1);
         }
         if(classBean.getAttendancegroup_day3_attendancetimespanid().equals("0")){

         }else
         {

            TimeBean day1 = new TimeBean("每周三","请选择班次");
            day1.selected =true;
            ClassSpan  classSpan = new ClassSpan();
            classSpan.setAttendacetimespan_begintime(classBean.getAttendancetimespan_day3().get(0).getAttendacetimespan_begintime());
            classSpan.setAttendacetimespan_endtime(classBean.getAttendancetimespan_day3().get(0).getAttendacetimespan_endtime());
            classSpan.setAttendacetimespan_storeno(classBean.getAttendancetimespan_day3().get(0).getAttendacetimespan_storeno());
            classSpan.setAttendacetimespan_timelength(classBean.getAttendancetimespan_day3().get(0).getAttendacetimespan_timelength());
            classSpan.setAttendacetimespanname(classBean.getAttendancetimespan_day3().get(0).getAttendacetimespanname());
            classSpan.setAttendacetimespanid(classBean.getAttendancetimespan_day3().get(0).getAttendacetimespanid());
            day1.setClassSpan(classSpan);
            arrayList.add(day1);
         }
         if(classBean.getAttendancegroup_day4_attendancetimespanid().equals("0")){

         }else
         {

            TimeBean day1 = new TimeBean("每周四","请选择班次");
            day1.selected =true;
            ClassSpan  classSpan = new ClassSpan();
            classSpan.setAttendacetimespan_begintime(classBean.getAttendancetimespan_day4().get(0).getAttendacetimespan_begintime());
            classSpan.setAttendacetimespan_endtime(classBean.getAttendancetimespan_day4().get(0).getAttendacetimespan_endtime());
            classSpan.setAttendacetimespan_storeno(classBean.getAttendancetimespan_day4().get(0).getAttendacetimespan_storeno());
            classSpan.setAttendacetimespan_timelength(classBean.getAttendancetimespan_day4().get(0).getAttendacetimespan_timelength());
            classSpan.setAttendacetimespanname(classBean.getAttendancetimespan_day4().get(0).getAttendacetimespanname());
            classSpan.setAttendacetimespanid(classBean.getAttendancetimespan_day4().get(0).getAttendacetimespanid());
            day1.setClassSpan(classSpan);
            arrayList.add(day1);
         }
         if(classBean.getAttendancegroup_day5_attendancetimespanid().equals("0")){

         }else
         {

            TimeBean day1 = new TimeBean("每周五","请选择班次");
            day1.selected =true;
            ClassSpan  classSpan = new ClassSpan();
            classSpan.setAttendacetimespan_begintime(classBean.getAttendancetimespan_day5().get(0).getAttendacetimespan_begintime());
            classSpan.setAttendacetimespan_endtime(classBean.getAttendancetimespan_day5().get(0).getAttendacetimespan_endtime());
            classSpan.setAttendacetimespan_storeno(classBean.getAttendancetimespan_day5().get(0).getAttendacetimespan_storeno());
            classSpan.setAttendacetimespan_timelength(classBean.getAttendancetimespan_day5().get(0).getAttendacetimespan_timelength());
            classSpan.setAttendacetimespanname(classBean.getAttendancetimespan_day5().get(0).getAttendacetimespanname());
            classSpan.setAttendacetimespanid(classBean.getAttendancetimespan_day5().get(0).getAttendacetimespanid());
            day1.setClassSpan(classSpan);
            arrayList.add(day1);
         }
         if(classBean.getAttendancegroup_day6_attendancetimespanid().equals("0")){

         }else
         {

            TimeBean day1 = new TimeBean("每周六","休息");
            day1.selected =true;
            ClassSpan  classSpan = new ClassSpan();
            classSpan.setAttendacetimespan_begintime(classBean.getAttendancetimespan_day6().get(0).getAttendacetimespan_begintime());
            classSpan.setAttendacetimespan_endtime(classBean.getAttendancetimespan_day6().get(0).getAttendacetimespan_endtime());
            classSpan.setAttendacetimespan_storeno(classBean.getAttendancetimespan_day6().get(0).getAttendacetimespan_storeno());
            classSpan.setAttendacetimespan_timelength(classBean.getAttendancetimespan_day6().get(0).getAttendacetimespan_timelength());
            classSpan.setAttendacetimespanname(classBean.getAttendancetimespan_day6().get(0).getAttendacetimespanname());
            classSpan.setAttendacetimespanid(classBean.getAttendancetimespan_day6().get(0).getAttendacetimespanid());
            day1.setClassSpan(classSpan);
            arrayList.add(day1);
         }

         if(classBean.getAttendancegroup_day7_attendancetimespanid().equals("0")){

         }else
         {

            TimeBean day1 = new TimeBean("每周日","休息");
            day1.selected =true;
            ClassSpan  classSpan = new ClassSpan();
            classSpan.setAttendacetimespan_begintime(classBean.getAttendancetimespan_day7().get(0).getAttendacetimespan_begintime());
            classSpan.setAttendacetimespan_endtime(classBean.getAttendancetimespan_day7().get(0).getAttendacetimespan_endtime());
            classSpan.setAttendacetimespan_storeno(classBean.getAttendancetimespan_day7().get(0).getAttendacetimespan_storeno());
            classSpan.setAttendacetimespan_timelength(classBean.getAttendancetimespan_day7().get(0).getAttendacetimespan_timelength());
            classSpan.setAttendacetimespanname(classBean.getAttendancetimespan_day7().get(0).getAttendacetimespanname());
            classSpan.setAttendacetimespanid(classBean.getAttendancetimespan_day7().get(0).getAttendacetimespanid());
            day1.setClassSpan(classSpan);
            arrayList.add(day1);
         }
         if(arrayList.size()>0){


            createSheetGroupAdapter.chooseTimeBean =new ChooseTimeBean();
            createSheetGroupAdapter.chooseTimeBean.timeBeans = arrayList;
         }

         if(classBean.getAttendanceuserlist()!=null&&classBean.getAttendanceuserlist().size()>0){
            ArrayList<String> members  =new ArrayList<String>(Arrays.asList(StringUtils.split(classBean.getAttendancegroupmembers(), ",")));

            ArrayList<EmployerBean> employerBeans=new ArrayList<>();
            for(int i =0;i<classBean.getAttendanceuserlist().size();i++){
               EmployerBean employerBean = new EmployerBean();
               employerBean.setUserid(classBean.getAttendanceuserlist().get(i).getUserid());
               employerBean.setUsername(classBean.getAttendanceuserlist().get(i).getUsername());
               employerBean.setUsernickname(classBean.getAttendanceuserlist().get(i).getUsernickname());
               employerBean.selected =true;

               employerBeans.add(employerBean);
            }
            createSheetGroupAdapter.members = employerBeans;

         }

//

      }

      createSheetGroupAdapter.timeListener = new TimeChooseAdapter.TimeListener() {
         @Override
         public void editClick(int ChildPosition) {

         }

         @Override
         public void doneClick() {

            if(StringUtils.isEmpty(createSheetGroupAdapter.groupName) ){

               ToastUtil.toast(CreateAttanceGroupActivity.this,"请输入考勤组名称");
               return;
            }

            GroupParam param = new GroupParam();
            param.groupname = createSheetGroupAdapter.groupName;
            ArrayList<String> ids = new ArrayList<>();
            if(createSheetGroupAdapter.members.size()>0){


               for(int i=0;i<createSheetGroupAdapter.members.size();i++){
                  ids.add(createSheetGroupAdapter.members.get(i).getUserid());

               }





            }
            param.members = StringUtils.join(ids,",");
            param.radius = createSheetGroupAdapter.span+"";
            param.overtimerange = createSheetGroupAdapter.time;

            ArrayList<String> add = new ArrayList<>();
            if(createSheetGroupAdapter.getData().size()>0){

               for(int i=0;i<createSheetGroupAdapter.getData().size();i++){
                 PoiItem poiItem = createSheetGroupAdapter.getData().get(i);
                  String address = poiItem.getTitle()+","+poiItem.getSnippet()+","+poiItem.getLatLonPoint().getLatitude()+","+poiItem.getLatLonPoint().getLongitude();

                  add.add(address);

               }

            }
            param.regions=  StringUtils.join(add,"|");

            if(createSheetGroupAdapter.classSpan!=null){
               param.attendancetimespanid =createSheetGroupAdapter.classSpan.getAttendacetimespanid();
            }else {
               param.attendancetimespanid ="0";
            }

            for (int i=0;i<createSheetGroupAdapter.chooseTimeBean.timeBeans.size();i++){

               TimeBean bean =createSheetGroupAdapter.chooseTimeBean.timeBeans.get(i);
               if(bean.getName().contains("一")){
                  param.day1_attendancetimespanid =bean.getClassSpan().getAttendacetimespanid();
               }else if(bean.getName().contains("二")){
                  param.day2_attendancetimespanid =bean.getClassSpan().getAttendacetimespanid();
               }else if(bean.getName().contains("三")){
                  param.day3_attendancetimespanid =bean.getClassSpan().getAttendacetimespanid();
               }else if(bean.getName().contains("四")){
                  param.day4_attendancetimespanid =bean.getClassSpan().getAttendacetimespanid();
               }else if(bean.getName().contains("五")){
                  param.day5_attendancetimespanid =bean.getClassSpan().getAttendacetimespanid();
               }else if(bean.getName().contains("六")){
                  param.day6_attendancetimespanid =bean.getClassSpan().getAttendacetimespanid();
               }else if(bean.getName().contains("日")){
                  param.day7_attendancetimespanid =bean.getClassSpan().getAttendacetimespanid();
               }


            }


            if(classBean==null){
               param.groupno = StringUtil.radom3String();
               addSubscription(listViewModel.addGroup(param));
            }else {
               param.groupno =classBean.getAttendancegroupno();
               addSubscription(listViewModel.updateGroup(param));
            }





         }
      };
      refreshLayout.setEnableLoadMore(false);
      refreshLayout.setEnableRefresh(false);
      recycleView.setAdapter(createSheetGroupAdapter);

      listViewModel.addGroup.observe(this,res->{
         finish();
         MessageEvent messageEvent=  new MessageEvent(MessageEvent.MESSAGE_EVENT_CREATE_GROUP);


         EventBus.getDefault().post(messageEvent);
      });
      listViewModel.updateGroup.observe(this,res->{
         finish();
         MessageEvent messageEvent=  new MessageEvent(MessageEvent.MESSAGE_EVENT_CREATE_GROUP);


         EventBus.getDefault().post(messageEvent);
      });
   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if(requestCode==1000&&resultCode==100){


         Gson gson =new Gson();
         ArrayList arrayList = gson.fromJson(data.getStringExtra("data"),ArrayList.class);

         createSheetGroupAdapter.members.clear();
         for (int i =0;i<arrayList.size();i++){
            EmployerBean employerBean = gson.fromJson((gson.toJson(arrayList.get(i))),EmployerBean.class);
            createSheetGroupAdapter.members.add(employerBean);

         }
         createSheetGroupAdapter.notifyDataChanged();
      }else if(requestCode==1001&&resultCode==100){
         Gson gson =new Gson();
         ClassSpan classSpan = gson.fromJson(data.getStringExtra("data"),ClassSpan.class);
         createSheetGroupAdapter.classSpan = classSpan;
         createSheetGroupAdapter.notifyDataChanged();

      }else if(requestCode==1002&&resultCode==100){
         Gson gson =new Gson();
         ChooseTimeBean classSpan = gson.fromJson(data.getStringExtra("data"),ChooseTimeBean.class);
         createSheetGroupAdapter.chooseTimeBean = classSpan;
         createSheetGroupAdapter.notifyDataChanged();

      }else if(requestCode==1003&&resultCode==100){
         Gson gson =new Gson();
         AddressBean poiItem = gson.fromJson(data.getStringExtra("data"),AddressBean.class);
         createSheetGroupAdapter.getData().addAll(poiItem.address) ;
         createSheetGroupAdapter.notifyDataChanged();

      }

   }

}
